package conf

import (
	"GoMaestro/internal/cli"
	"log/slog"
	"os"

	"gopkg.in/yaml.v3"
)

type Config struct {
	//Log struct {
	//	Level string `yaml:"level"`
	//} `yaml:"log"`
	Logfile string `yaml:"logfile"`
	Web     struct {
		Address string `yaml:"address"`
	} `yaml:"web"`
	DB struct {
		MysqlUser         string `yaml:"mysql_user"`
		MysqlPassword     string `yaml:"mysql_password"`
		MysqlHost         string `yaml:"mysql_host"`
		MysqlPort         int    `yaml:"mysql_port"`
		MysqlDatabasename string `yaml:"mysql_databasename"`
	} `yaml:"db"`
	Job map[string]struct {
		Cron string `yaml:"cron"`
	} `yaml:"job"`
	Jenkins struct {
		Address  string `yaml:"address"`
		Username string `yaml:"username"`
		Password string `yaml:"password"`
		Token    string `yaml:"token"`
	} `yaml:"jenkins"`
}

func Init() {
	yamlData, err := os.ReadFile(cli.ConfigFilePath)
	if err != nil {
		slog.Error("read conf file error", slog.Any("error", err))
		os.Exit(5)
	}

	err = yaml.Unmarshal(yamlData, &C)
	if err != nil {
		slog.Error("yaml unmarshal error", slog.Any("error", err))
		os.Exit(5)
	}
	slog.Info("load conf successfully")
}
